package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasProcessTemplateNodeDao;
import com.xbongbong.paas.domain.entity.PaasProcessTemplateNodeEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasProcessTemplateNodeEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasProcessTemplateNodeModel;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.StringConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wufeng
 * @date: 2018/8/13 15:22
 * @desrcption: PaaS流程设计实现类
 */
@Service("paasProcessTemplateNodeModel")
public class PaasProcessTemplateNodeModelImpl implements PaasProcessTemplateNodeModel {

    private static final String COLUMNS = " id, corpid, app_id, menu_id, form_id, template_id, prev_id, priority, `condition`, condition_type, wrapper_id,type, name, uid, field_permission, " +
            "main_user,enable_cc_flag, cc_user, opinion_flag, enable, creator_id, commit_flag, commit_text, storage_flag, storage_text, " +
            "commit_print_flag, commit_print_text, back_flag,back_type, back_text, back_node, transfer_flag, transfer_text, transfer_user, " +
            "end_flag, end_text, commit_verify_flag, commit_verify_condition, sign_type, timeout_handle, print_template_id," +
            " cc_print_flag, x, y, add_time, update_time, del ";


    @Resource
    private PaasProcessTemplateNodeDao processTemplateNodeDao;

    @Override
    public Integer insert(PaasProcessTemplateNodeEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return processTemplateNodeDao.insert(entity);
    }

    @Override
    public List<PaasProcessTemplateNodeEntityExt> list(Map<String, Object> map) {
        map.put("columns", new StringBuffer(COLUMNS));
        return processTemplateNodeDao.findEntitys(map);
    }

    @Override
    public List<PaasProcessTemplateNodeEntityExt> getNodeByTemplateIdList(Long templateId, String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("templateId", templateId);
        return list(param);
    }

    @Override
    public PaasProcessTemplateNodeEntityExt getByKey(Integer key, String corpid) {
        return processTemplateNodeDao.getByKey(key, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return processTemplateNodeDao.getEntitysCount(map);
    }

    @Override
    public Integer delete(PaasProcessTemplateNodeEntity entity) {
        return  processTemplateNodeDao.deleteByKey(entity.getId().intValue(), entity.getCorpid());
    }

    @Override
    public Integer update(PaasProcessTemplateNodeEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return processTemplateNodeDao.update(entity);
    }

    @Override
    public void insertBatch(List<PaasProcessTemplateNodeEntityExt> nodeEntityUpdateList) {
        List<PaasProcessTemplateNodeEntityExt> list = new ArrayList<>();
        for (int i = 0; i < nodeEntityUpdateList.size() ; i++) {
            list.add(nodeEntityUpdateList.get(i));
            if (list.size() == 10 || i == nodeEntityUpdateList.size() - 1) {
                processTemplateNodeDao.insertBatch(list);
                list.clear();
            }
        }
    }

    @Override
    public Integer updateBatch(List<PaasProcessTemplateNodeEntityExt> nodeEntityUpdateList, String corpid) {
        List<PaasProcessTemplateNodeEntityExt> list = new ArrayList<>();
        for (int i = 0; i < nodeEntityUpdateList.size() ; i++) {
            list.add(nodeEntityUpdateList.get(i));
            if (list.size() == 20 || i == nodeEntityUpdateList.size() - 1) {
                processTemplateNodeDao.updateBatch(list, corpid);
                list.clear();
            }
        }
        return nodeEntityUpdateList.size();
    }

    @Override
    public Integer deleteBatch(List<PaasProcessTemplateNodeEntityExt> nodeEntityUpdateList, String corpid) {
        return processTemplateNodeDao.deleteBatch(nodeEntityUpdateList, corpid);
    }

    @Override
    public List<PaasProcessTemplateNodeEntityExt> findEntitysByNodeList(List<PaasFormDataEntityExt> formDataList, Map<String, Object> param) {
        param.put("columns", new StringBuffer(COLUMNS));
        return processTemplateNodeDao.findEntitysByNodeList(formDataList, param);
    }
    
    @Override
    public Integer updateBatchById(List<PaasProcessTemplateNodeEntityExt> nodeEntityUpdateList) {
        return processTemplateNodeDao.updateBatchById(nodeEntityUpdateList);
    }

    @Override
    public Integer deleteByFormId(Long formId, String corpid) {
        return processTemplateNodeDao.deleteByFormId(formId, corpid);
    }
}
